define(function (require) {
    require('artdialog');
    require('hash');
    require('listTable');
    require('treeTable');
    require('jq_tools');
    require('colorpicker');

    var Base = require('group/common/main');
    var utils = require('common/utils/admin');
    var echarts = require('common/echarts');

    return Base.extend({
        initialize: function (params) {
            var _this = this;
            Base.prototype.initialize.apply(_this, [params]);
            _this.initPage();
        },
        initPage: function () {
            var _this = this;
            $(".J_preview").preview();
            $('.J_tablelist').listTable();
            _this.create_datepicker();
            var pageFunc = _.sprintf("page_%s_%s", _global.m, _global.a);
            // console.log("执行页面函数：" + pageFunc);
            if (_this[pageFunc]) {
                _this[pageFunc].apply(_this, []);
                _this.validate();
            }
        },
        /*
         网站设置
         */
        page_setting_index: function () {
            var _this = this;
            $('.J_change_status').live('click', function () {
                if ($(this).val() == '0') {
                    $('#J_closed_reason').fadeIn();
                } else {
                    $('#J_closed_reason').fadeOut();
                }
            });
            $('#J_mail_test').live('click', function () {
                var email = $('#J_email').val();
                if (_.empty(email)) {
                    $('#J_mail_test_tip').addClass('red').html('请输入测试邮箱！');
                    return;
                }
                utils.ajaxGet({
                        a: 'ajax_mail_test',
                        email: email
                    },
                    function (resp) {
                        $('#J_mail_test_tip').removeClass('red').addClass('green').html('发送成功');
                    },
                    function (resp) {
                        $('#J_mail_test_tip').addClass('red').html('发送失败');
                    });
            });
        },
        /*
         登录注册
         */
        page_setting_user: function () {
            $('.J_change_status').live('click', function () {
                if ($(this).val() == '0') {
                    $('#J_closed_reason').fadeIn();
                } else {
                    $('#J_closed_reason').fadeOut();
                }
            });
        },
        /*
         seo配置
         */
        page_seo_page: function () {
            $('ul.J_tabs').tabs('div.J_panes > div');
        },
        /*
         文章列表
         */
        page_article_index: function () {
            $('.J_cate_select').cate_select({
                top_option: _global.lang.all
            });
        },
        /*
         文章编辑
         */
        page_article_edit: function () {
            var _this = this;
            _this.page_article_add();
            $('.del_img').live('click', function () {
                if (confirm("确定要删除图片吗")) {
                    _this = $(this);
                    var id = $('#id').val();
                    utils.ajaxGet({
                        a: 'ajax_del_img',
                        id: id
                    }, function (resp) {
                        _this.prev().remove();
                    }, function (resp) {
                        $.zhiphp.tip({
                            content: result.msg
                        });
                    });
                }
            });
        },
        /*
         文章添加
         */
        page_article_add: function () {
            var _this = this;
            $('.J_cate_select').cate_select('请选择');
            _this.kindEditor('#info');
            $('ul.J_tabs').tabs('div.J_panes > div');
            //颜色选择器
            $('.J_color_picker').colorpicker();
            //自动获取标签
            $('#J_gettags').live('click', function () {
                var title = $.trim($('#J_title').val());
                if (_.empty(title)) {
                    $.zhiphp.tip({
                        content: _global.lang.article_title_isempty,
                        icon: 'alert'
                    });
                    return;
                }
                utils.ajaxGet({
                    a: 'ajax_gettags',
                    title: title
                }, function (resp) {
                    $('#J_tags').val(result.data);
                }, function (resp) {
                    $.zhiphp.tip({
                        content: result.msg
                    });
                });
            });
            $('.del_img').live('click', function () {
                if (confirm("确定要删除图片吗")) {
                    _this = $(this);
                    var id = $('#id').val();
                    $.getJSON(_.U({
                        a: 'ajax_del_img'
                    }), {
                        id: id
                    }, function (result) {
                        if (result.status == 1) {
                            _this.prev().remove();
                        } else {
                            $.zhiphp.tip({
                                content: result.msg
                            });
                        }
                    });
                }
            });
        },
        /*
         单页列表
         */
        page_article_page: function () {
            $("#J_cate_tree").treeTable({
                indent: 20,
                treeColumn: 1,
                clickableNodeNames: true,
                initialState: 'expanded'
            });
        },
        /*
         单页编辑
         */
        page_article_page_edit: function () {
            var _this = this;
            _this.kindEditor('#J_info');
            $('ul.J_tabs').tabs('div.J_panes > div');
        },
        /*
         文章分类
         */
        page_article_cate_index: function () {
            var _this = this;
            $("#J_cate_tree").treeTable({
                indent: 20,
                treeColumn: 1
            });
            _this.initFormDialog(function ($$) {
                $$('.J_cate_select').cate_select();
                _this.uploader({
                    input: $('#J_img'),
                    btn: $('#J_upload_img')
                });
            });
        },
        /*
         缓存管理
         */
        page_cache_index: function () {
            $('#J_clear_cache').live('click', function () {
                $('input[name="type"]:checked').each(function () {
                    var type = $(this).val();
                    uri = $(this).attr('data-uri');
                    $('#' + type + '_ifm').html(_global.lang.clear_wait);
                    $.getJSON(uri, {
                        type: type
                    }, function (result) {
                        $('#' + type + '_ifm').addClass('onCorrect').html(_global.lang.clear_success);
                    });
                });
            });
        },
        /*
         数据恢复
         */
        page_backup_restore: function () {
            $(".show_sub").click(function () {
                $(this).attr("src", function () {
                    if (this.src == _global.assets + '/images/tv-expandable.gif') {
                        return _global.assets + '/images/tv-collapsable.gif';
                    } else {
                        return _global.assets + '/images/tv-expandable.gif';
                    }
                });
                var sub_id = $(this).attr('sub');
                $("tr[parent='" + sub_id + "']").toggle();
            });
            $('.expander').toggle(function () {
                $(this).parent().parent().removeClass('collapsed').addClass('expanded');
                $('tr[parent="' + $(this).attr('name') + '"]').show();
            }, function () {
                $(this).parent().parent().removeClass('expanded').addClass('collapsed');
                $('tr[parent="' + $(this).attr('name') + '"]').hide();
            });
        },
        /*
         城市列表
         */
        page_region_index: function () {
            var _this = this;
            $("#J_cate_tree").treeTable({
                indent: 20,
                treeColumn: 2
            });
            _this.initFormDialog(function ($$) {
                $$('.J_cate_select').cate_select();
            });
        },
        /*
         车辆添加
         */
        page_user_vehicle_add: function () {
            var _this = this;
            $('ul.J_tabs').tabs('div.J_panes > div');
            //上传图片
            _this.uploader({
                input: $('#J_img'),
                btn: $('#J_upload_img')
            });
            $('.J_searchUserList').click(function () {
                var username = _.trim($('#username').val());
                if (_.empty(username)) {
                    return;
                }
                $.ajax({
                    url: _.U({
                        m: 'user_vehicle',
                        a: 'search_user'
                    }),
                    type: 'POST',
                    data: {
                        username: username
                    },
                    success: function (uhtml) {
                        $('#uid').show().html(uhtml);
                    }
                });
            });
        },
        /*
         车辆编辑
         */
        page_user_vehicle_edit: function () {
            this.page_user_vehicle_add();
        },
        /*
         公司添加
         */
        page_company_add: function () {
            var _this = this;
            _this.kindEditor('#info');
            $('ul.J_tabs').tabs('div.J_panes > div');
            _this.uploader({
                input: $('#J_img'),
                btn: $('#J_upload_img')
            });
            _this.uploader({
                input: $('#J_ban_img'),
                btn: $('#J_ban_upload_img'),
                action: {
                    name: 'banner'
                }
            });
        },
        /*
         公司编辑
         */
        page_company_edit: function () {
            var _this = this;
            _this.page_company_add();
        },
        page_company_favs: function () {
            $('.J_searchCompanyList').click(function () {
                var cname = $('#cname').val();
                if (_.empty(cname)) {
                    return false;
                }
                var url = '{:u("company/search_company")}';
                $.ajax({
                    url: url,
                    type: 'POST',
                    data: {
                        cname: cname
                    },
                    success: function (uhtml) {
                        $('#cid').show().html(uhtml);
                    }
                });
            });
        },
        /*
         品牌添加
         */
        page_brand_add: function () {
            var _this = this;
            _this.kindEditor('#content');
            $('ul.J_tabs').tabs('div.J_panes > div');
            _this.uploader({
                input: $('#J_img'),
                btn: $('#J_upload_img')
            });
        },
        /*
         品牌编辑
         */
        page_brand_edit: function () {
            this.page_brand_add();
        },
        /*
         方案添加
         */
        page_scheme_add: function () {
            $('ul.J_tabs').tabs('div.J_panes > div');
            $('.J_searchUserList').click(function () {
                var username = $('#username').val();
                if (_.empty(username)) {
                    return;
                }
                var url = _.U({
                    a: 'search_user'
                });
                $.ajax({
                    url: url,
                    type: 'POST',
                    data: {
                        username: username
                    },
                    success: function (uhtml) {
                        $('#uid').show().html(uhtml);
                    }
                });
            });
        },
        /*
         方案编辑
         */
        page_scheme_edit: function () {
            this.page_scheme_add();
        },
        /*
         广告列表
         */
        page_ad_index: function () {
            var _this = this;
            _this.initFormDialog(function ($$) {
                var id = $$('form input[name=id]').val();
                var is_change = false;
                if (id > 0) {
                    $$("#type").change(function () {
                        if (!is_change) {
                            if (confirm("更改类型将删除旧的广告内容（图片、代码、flsah等等）")) {
                                $.post("{:U(MODULE_NAME.'/ajax_edit')}", {
                                    field: 'content',
                                    val: ' ',
                                    id: id
                                });
                                is_change = true;
                            }
                        }
                        $$(".bill_media").hide();
                        $$("#ad_" + $(this).val()).show();
                        $$('.bill_media input').val("");
                        $$('.bill_media textarea').html("");
                    });
                } else {
                    $$("#type").change(function () {
                        $$(".bill_media").hide();
                        $$("#ad_" + $(this).val()).show();
                    });
                    $$("#type").change();
                    //获取版块允许的广告类型
                    $$("#board_id").change(function () {
                        var allowtype = $$("#board_id option:selected").attr('allowtype');
                        var allowtype_arr = allowtype.split("|");
                        $$("#type option").attr('selected', false).hide();
                        $$("#type option").each(function () {
                            for (i = 0; i < allowtype_arr.length; i++) {
                                if ($(this).val() == allowtype_arr[i]) {
                                    $(this).show();
                                }
                            }
                        });
                        $$('#type option:visible:first').attr('selected', true);
                        $$("#type").change();
                    });
                    $$("#board_id").change();
                }
                //上传图片
                _this.uploader({
                    input: $$('#J_img'),
                    btn: $$('#J_upload_img'),
                    action: {
                        name: 'img',
                        dirname: 'ad'
                    }
                });
                //上传banner图片
                _this.uploader({
                    input: $$('#J_banner'),
                    btn: $$('#J_upload_banner'),
                    action: {
                        name: 'banner',
                        dirname: 'ad'
                    }
                });
                _this.uploader({
                    input: $$('#J_extimg'),
                    btn: $$('#J_upload_extimg'),
                    action: {
                        type: 'extimg'
                    }
                });
                _this.uploader({
                    input: $$('#J_flash'),
                    btn: $$('#J_upload_flash'),
                    action: {
                        type: 'flash'
                    }
                });
            });
        },
        /*
         广告位列表
         */
        page_adboard_index: function () {
            var _this = this;
            _this.initFormDialog(function ($$) {
            });
        },
        /*
         用户管理
         */
        page_user_index: function () {
            var _this = this;
            _this.initFormDialog(function ($$) {
                var id = $$('input[name=id]').val();
                _this.uploader({
                    input: $$('#J_img'),
                    btn: $$('#J_upload_img'),
                    action: {
                        a: 'ajax_upload_imgs'
                    }
                });
            });
        },
        /*
         黑名单管理
         */
        page_ipban_index: function () {
            var _this = this;
            _this.initFormDialog(function ($$) {
            });
        },
        /*
         敏感词管理
         */
        page_badword_index: function () {
            var _this = this;
            _this.initFormDialog(function ($$) {
            });
        },
        /*
         菜单管理
         */
        page_menu_index: function () {
            var _this = this;
            _this.initFormDialog(function ($$) {
            });
        },
        /*
         登录接口
         */
        page_oauth_index: function () {
            var _this = this;
            _this.initFormDialog(function ($$) {
            });
        },
        /*
         管理员列表
         */
        page_admin_index: function () {
            var _this = this;
            _this.initFormDialog(function ($$) {
            });
        },
        /*
         管理员角色列表
         */
        page_admin_role_index: function () {
            var _this = this;
            _this.initFormDialog(function ($$) {
            });
        },
        /*
         授权列表
         */
        page_admin_role_auth: function () {
            var _this = this;
            $("#J_auth_tree").treeTable({
                indent: 20
            });

            $('.J_checkall').live('click', function () {
                $('.J_checkitem').attr('checked', this.checked);
                $('.J_checkall').attr('checked', this.checked);
            });

            $('.J_checkitem').live('click', function () {
                var chk = $("input[type='checkbox']"),
                    count = chk.length,
                    num = chk.index($(this)),
                    level_top = level_bottom = chk.eq(num).attr('level');
                for (var i = num; i >= 0; i--) {
                    var le = chk.eq(i).attr('level');
                    if (eval(le) < eval(level_top)) {
                        chk.eq(i).attr("checked", true);
                        level_top = level_top - 1;
                    }
                }
                for (var j = num + 1; j < count; j++) {
                    le = chk.eq(j).attr('level');
                    if (chk.eq(num).attr("checked")) {
                        if (eval(le) > eval(level_bottom)) chk.eq(j).attr("checked", true);
                        else if (eval(le) == eval(level_bottom)) break;
                    } else {
                        if (eval(le) > eval(level_bottom)) chk.eq(j).attr("checked", false);
                        else if (eval(le) == eval(level_bottom)) break;
                    }
                }
            });
        },
        /*
         链接列表
         */
        page_flink_index: function () {
            var _this = this;
            _this.initFormDialog(function ($$) {
                _this.uploader({
                    input: $$('#J_img'),
                    btn: $$('#J_upload_img')
                });
            });
        },
        /*
         链接分类列表
         */
        page_flink_cate_index: function () {
            var _this = this;
            _this.initFormDialog(function ($$) {
            });
        },
        /*
         批量注册
         */
        page_auto_user_index: function () {
            var _this = this;
            _this.initFormDialog(function ($$) {
            });
        },
        /*
         查询次数统计
         */
        page_statistic_searchMonth: function () {
            utils.ajaxGet({}, function (resp) {
                echarts.line('#chart', {
                    data: resp.info.data,
                    config: {
                        series: {
                            count: {
                                name: '查询次数',
                                color: '#f15755'
                            }
                        }
                    }
                });
            });
        },
        /*
         查询地区统计
         */
        page_statistic_searchArea: function () {
            utils.ajaxGet({}, function (resp) {
                echarts.map_province('#chart', {
                    data: resp.info.province_data,
                    config: {
                        series: {
                            name: '查询车辆'
                        }
                    }
                });
            });
        },
        /*
         查询会员地区统计
         */
        page_statistic_userArea: function () {
            utils.ajaxGet({}, function (resp) {
                $('.loading').removeClass('loading');
                var info = resp.info;
                echarts.map_province('#user_province_chart', {
                    data: info.user_province_data,
                    config: {
                        series: {
                            name: '会员数'
                        }
                    }
                });
            });
        },
        /*
         查询会有性别统计
         */
        page_statistic_userSex: function () {
            utils.ajaxGet({}, function (resp) {
                $('.loading').removeClass('loading');
                var info = resp.info;
                echarts.pie('#user_sex_chart', {
                    data: info.user_sex_data,
                    total: info.user_sex_total,
                    config: {
                        series: {
                            sex_0: {
                                name: '未知',
                                color: '#f15755'
                            },
                            sex_1: {
                                name: '男',
                                color: '#5d9cec'
                            },
                            sex_2: {
                                name: '女',
                                color: '#62c87f'
                            }
                        }
                    }
                });
            });
        },
        /*
         查询会员注册日期统计
         */
        page_statistic_userRegister: function () {
            utils.ajaxGet({}, function (resp) {
                $('.loading').removeClass('loading');
                var info = resp.info;
                echarts.line('#user_register_chart', {
                    data: info.user_register_data,
                    config: {
                        series: {
                            count: {
                                name: '注册数',
                                color: '#f15755'
                            }
                        }
                    }
                });
            });
        }
    });
});